<?php

namespace App\Admin\Controllers;

use App\Models\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Content;

class MiniAppPateController extends AdminController
{
    public function index(Content $content)
    {
        return $content->body($this->form());
    }

    protected function form()
    {
        $form = new Form();

        $form->title('小程序页面');
        $form->action(admin_route('mini_app_page.store'));

        $data = Settings::where('group', 'wechat')->where('name', 'page_path_data')->value('value');


        $form->editor('page_path_data', '内容')->value($data);

        $form->disableListButton();
        $form->disableResetButton();
        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();

        return $form;
    }

    public function store()
    {
        $data = request()->input('page_path_data');

        Settings::updateOrCreate(
            ['group' => 'wechat', 'name' => 'page_path_data'],
            ['value' => $data]
        );

        return JsonResponse::make()->success('保存成功！')->refresh();
    }
}
